<template>
    <div class="light-Ct">
        <!-- 横向Dom -->
        <div class="item-Cd item-Cd-Dom1">
            <div @click="seeNodeDetail(index, item)" class="light-circle" style="position: relative;" :class="{
                'light-green': item.nodeState == 1,
                'light-red': item.nodeState == 0,
                'light-orange': item.nodeState == 2,
            }">
                {{ item.percent }}
            </div>

            <div class="item-name">
                <div v-if="!isEditing" @click="startEdit" class="pointers">{{ item.nodeName }}</div>
                <el-input v-else v-model="editedName" @blur="finishEdit" @keyup.enter="finishEdit" ref="editInput"
                    style="width: 200px;" />
            </div>

            <div class="operation">
                <a @click="addBrotherItem(index, item)" v-if="item.projectId !== 0">同级 +</a>
                <span v-if="item.projectId !== 0">|</span>
                <a @click="addChildItem(index, item)">子级 +</a>
                <span>|</span>
                <a @click="deleteItem(index, item)">删除</a>
            </div>

            <div class="item-info">
                <p>开始时间: {{ formatDateTime(item.nodeStartTime) }}</p>
                <p>结束时间: {{ formatDateTime(item.nodeEndTime) }}</p>
            </div>

            <!-- <div style="margin-top: 10px;">id: {{ item.id }}</div>
            <div>nodeState: {{ item.nodeState }}</div> -->

            <div class="item-btn">
                <a @click="testIntem(index, item)">编辑</a>
                <span v-if="item.children && item.children.length">|</span>
                <a v-if="item.children && item.children.length" @click="expandCollapse(index, item)">
                    {{ item.expanded ? '收起' : '展开' }}
                </a>
            </div>

        </div>

        <!-- 纵向Dom -->
        <div class="item-Cd item-Cd-Dom2">

            <div class="item-Cd-title">
                <div @click="seeNodeDetail(index, item)" class="light-circle" style="position: relative;" :class="{
                    'light-green': item.nodeState == 1,
                    'light-red': item.nodeState == 0,
                    'light-orange': item.nodeState == 2,
                }">
                    {{ item.percent }}
                </div>
                <div class="item-name">
                    <div v-if="!isEditing" @click="startEdit" class="pointers">{{ item.nodeName }}</div>
                    <el-input v-else v-model="editedName" @blur="finishEdit" @keyup.enter="finishEdit" ref="editInput"
                        style="width: 200px;" />
                </div>
            </div>
            <div class="operation">
                <a @click="addBrotherItem(index, item)" v-if="item.projectId !== 0">同级 +</a>
                <span v-if="item.projectId !== 0">|</span>
                <a @click="addChildItem(index, item)">子级 +</a>
                <span>|</span>
                <a @click="deleteItem(index, item)">删除</a>
                <span>|</span>
                <a @click="testIntem(index, item)">编辑</a>
                <span v-if="item.children && item.children.length">|</span>
                <a v-if="item.children && item.children.length" @click="expandCollapse(index, item)">
                    {{ item.expanded ? '收起' : '展开' }}
                </a>
            </div>

            <div class="item-info">
                <p>开始时间: {{ formatDateTime(item.nodeStartTime) }}</p>
                <p>结束时间: {{ formatDateTime(item.nodeEndTime) }}</p>
            </div>

            <!-- <div style="margin-top: 10px;">id: {{ item.id }}</div>
            <div>nodeState: {{ item.nodeState }}</div> -->

        </div>

        <div v-if="item.expanded && item.children && item.children.length"
            :class="item.children && item.children.length ? 'childItem childItemSon' : 'childItem'">
            <RecursiveItem v-for="(child, childIndex) in item.children" :key="childIndex" :item="child"
                :index="childIndex" @editItem="editItem" @addBrotherItem="addBrotherItem" @addChildItem="addChildItem"
                @deleteItem="deleteItem" @testIntem="testIntem" @seeNodeDetail="seeNodeDetail"
                @expandCollapse="expandCollapse" />
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted, defineProps, defineEmits, onUnmounted, nextTick } from 'vue';
import RecursiveItem from './RecursiveItem.vue';
import { formatDateTime } from "@/utils/format";
// import { ElMessage } from 'element-plus';

const props = defineProps({
    item: {
        type: Object,
        required: true
    },
    index: {
        type: Number,
        required: true
    },
});

const emit = defineEmits(['testIntem', 'editItem', 'addBrotherItem', 'addChildItem', 'deleteItem', 'seeNodeDetail', 'expandCollapse']);

const isEditing = ref(false);
const editedName = ref(props.item.nodeName);
const editInput = ref(null);

const startEdit = () => {
    isEditing.value = true;
    nextTick(() => {
        if (isEditing.value && editInput.value) {
            editInput.value.focus();
        }
    });
};

const finishEdit = () => {
    isEditing.value = false;
    if (!editedName.value) {
        // editedName.value = props.item.nodeName;
        return //ElMessage.error("请输入节点名称");
    }
    if (editedName.value !== props.item.nodeName) {
        emit('editItem', props.index, { ...props.item, nodeName: editedName.value });
    }
};

const testIntem = (index, item) => {
    emit('testIntem', index, item);
};

const editItem = (index, item) => {
    emit('editItem', index, item);
};

const addBrotherItem = (index, item) => {
    emit('addBrotherItem', index, item);
};

const addChildItem = (index, item) => {
    emit('addChildItem', index, item);
};

const deleteItem = (index, item) => {
    emit('deleteItem', index, item);
};

const seeNodeDetail = (index, item) => {
    emit('seeNodeDetail', index, item)
};

const expandCollapse = (index, item) => {
    emit('expandCollapse', index, item);
};

onMounted(() => {

});

onUnmounted(() => {

});

</script>

<style lang="scss" scoped>
.pointers {
    cursor: pointer;
}

// 横向样式
.light-item {
    .light-Ct {
        .item-Cd {
            padding: 10px;
            // margin: 0px 10px;
            position: relative;
            // min-width: 200px;
            min-width: 170px;
            font-size: 14px;

            &.item-Cd-Dom2 {
                display: none;
            }

            .light-circle {
                width: 30px;
                height: 30px;
                border-radius: 50%;
                background-color: gray;
                cursor: pointer;
                line-height: 30px;
                margin: auto;
                color: black;

                &.light-green {
                    background-color: green;
                }

                &.light-red {
                    background-color: red;
                }

                &.light-orange {
                    background-color: orange;
                }

                &.light-gray {
                    background-color: gray;
                }
            }

            .item-info {
                font-size: 12px;
                width: 178px;
                margin: 20px auto 0px;
                text-align: left;
                line-height: 20px;

                p {
                    margin: 0px;
                }
            }

            .item-btn {
                margin-top: 20px;

                a {
                    color: var(--el-color-primary);
                    cursor: pointer;
                }

                span {
                    color: #eee;
                    margin: 0px 4px;
                }
            }

            .item-name {
                margin: 10px 0px;
            }

            .operation {
                a {
                    color: var(--el-color-primary);
                    cursor: pointer;
                }

                span {
                    color: #eee;
                    margin: 0px 4px;
                }
            }

        }
    }

    .childItem {
        display: flex;
    }
}

// 纵向样式
.light-item-2 {
    .light-Ct {
        margin: 10px 0px;

        .item-Cd {
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            background-color: #f9f9f9;
            transition: background-color 0.3s;
            min-width: 236px;

            &.item-Cd-Dom1 {
                display: none;
            }

            &:hover {
                background-color: #eee;
            }

            .item-Cd-title {
                display: flex;
                align-items: center;

                .light-circle {
                    width: 30px;
                    height: 30px;
                    line-height: 30px;
                    text-align: center;
                    border-radius: 50%;
                    margin-right: 10px;
                    color: white;

                    &.light-green {
                        background-color: green;
                    }

                    &.light-red {
                        background-color: red;
                    }

                    &.light-orange {
                        background-color: orange;
                    }
                }
            }

            .operation {
                margin-left: 40px;

                a {
                    color: #007bff;
                    text-decoration: none;
                    cursor: pointer;
                }

                span {
                    margin: 0px 4px;
                    color: #ccc;
                }
            }

            .item-info {
                margin: 10px 0px 0px 40px;
                font-size: 12px;
                p{
                    margin: 2px 0px;
                }
            }
        }

        .childItem {
            margin-left: 24px;
        }

        .childItemSon {
            border-left: 1px dashed #ccc;
            padding-left: 24px;
        }
    }
}
</style>
